package com.taikang.common.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {
	
	private static String timeFormat="yyyy-MM-dd hh:mm:ss";
	private static String dateFormat="yyyy-MM-dd";

	public static String formatDate(Date date) {
		if(date==null){
			return "";
		}
		SimpleDateFormat df = new SimpleDateFormat(dateFormat);
		String time = df.format(date);
		return time;

	}
	public static String formatTime(Date date) {
		return formatDate(date,timeFormat);
	}
	public static String formatDate(Date date, String formate) {
		// "yyyy-MM-dd hh:mm:ss"
		SimpleDateFormat df = new SimpleDateFormat(formate);
		String time = df.format(date);
		return time;
		
	}
	public static String formatDate(Timestamp  timestamp, String formate) {
		// "yyyy-MM-dd hh:mm:ss"
		Date date=new Date(timestamp.getTime());
		SimpleDateFormat df = new SimpleDateFormat(formate);
		String time = df.format(date);
		return time;
		
	}
	public static Date parseDate(String date, String format)  {
		// "yyyy-MM-dd hh:mm:ss"
		SimpleDateFormat df = new SimpleDateFormat(format);
		Date d=null;
		try {
			d = df.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}
	
	public static Date parseDate(String date)  {
		return parseDate(date,dateFormat);
	}
	public static Date parseTime(String date)  {
		return parseDate(date,timeFormat);
	}
	
	public static void main(String args[]){
		Date now=new Date();
		String dayTime=DateUtils.formatDate(now,"yyyyMMdd");
		String hourTime=DateUtils.formatDate(now,"hhmmss");
		System.out.println(dayTime+" " +hourTime);
		
	}

	//added by wanghuan 2009-9-22
	//计算两个日期之间有几天
	public static long getDateDistance(String startDate , String endDate)
	{
		long quot = 0;
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
		Date start;
		Date end;
		try {
			start = ft.parse(startDate);
			end = ft.parse(endDate);
			quot = end.getTime() - start.getTime();
			quot = quot / 1000 / 60 / 60 / 24;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return quot;
	}
	

	//end
}
